require 'feed-normalizer'

class FeedCatcher
  
  attr_accessor :blog, :xml, :feed
  
  def initialize(blog)
    @blog = blog
  end
  
  def fetch_feed
    @xml = open(@blog.feed_url)
#    puts "XML: #{@xml}"
  end
  
  def parse_feed
    @feed = FeedNormalizer::FeedNormalizer.parse(@xml) # open(@blog.feed_url)
  end
  
  def add_new

    @feed.entries.each do |entry|
      title = entry.title
      url = entry.urls.first
      puts "#{title} - #{url}"
      if !Link.find_by_url(url)
        link = @blog.links.create(:url => url, :title => title)
        puts "\tNew Link: #{link.id} - #{link.url} - #{link.title}"
      end
#      puts "Entry: #{entry.inspect}"
    end

  end

end
